package com.aim.common.handler.log.factory;

import com.aim.common.enums.LogType;
import com.aim.common.enums.StateEnum;
import com.aim.common.handler.FitContextHolder;
import com.aim.dao.system.SysLoginLogDao;
import com.aim.dao.system.SysOperationLogDao;
import com.aim.entity.system.SysLoginLog;
import com.aim.entity.system.SysOperationLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.TimerTask;

/**
 * 日志操作任务创建工厂
 */
public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogTaskFactory.class);
    private static SysLoginLogDao loginLogMapper = FitContextHolder.getBean(SysLoginLogDao.class);
    private static SysOperationLogDao operationLogMapper = FitContextHolder.getBean(SysOperationLogDao.class);

    public static TimerTask loginLog(final Long userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    SysLoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
                    loginLogMapper.save(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                SysLoginLog loginLog = LogFactory.createLoginLog(
                        LogType.LOGIN_FAIL, null, "账号:" + username + "," + msg, ip);
                try {
                    loginLogMapper.save(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败异常!", e);
                }
            }
        };
    }

    public static TimerTask exitLog(final Long userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                SysLoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip);
                try {
                    loginLogMapper.save(loginLog);
                } catch (Exception e) {
                    logger.error("创建退出日志异常!", e);
                }
            }
        };
    }

    public static TimerTask bizLog(final Long userId, final String bizName, final String clazzName, final String methodName, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                SysOperationLog operationLog = LogFactory.createOperationLog(
                        LogType.BUSSINESS, userId, bizName, clazzName, methodName, msg, StateEnum.SUCCESS);
                try {
                    operationLogMapper.save(operationLog);
                } catch (Exception e) {
                    logger.error("创建业务日志异常!", e);
                }
            }
        };
    }

    /**
     *
     * @param userId
     * @param exception
     * @return 返回异常
     */
    public static TimerTask excpLog(final Long userId, final Exception exception) {
        String msg = getExceptionMsg(exception);
        return excpLog(userId, msg);
    }

    public static TimerTask excpLog(final Long userId, final String msg) {
        return excpLog(userId, null, null, msg);
    }

    public static TimerTask excpLog(final Long userId, final String clazzName, final String methodName, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                SysOperationLog operationLog = LogFactory.createOperationLog(
                        LogType.EXCEPTION, userId, "", clazzName, methodName, msg, StateEnum.FAIL);
                try {
                    operationLogMapper.save(operationLog);
                } catch (Exception e) {
                    logger.error("创建异常日志异常!", e);
                }
            }
        };
    }

    private static String getExceptionMsg(Throwable e) {
        StringWriter sw = new StringWriter();
        try {
            e.printStackTrace(new PrintWriter(sw));
        } finally {
            try {
                sw.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return sw.getBuffer().toString().replaceAll("\\$", "T");
    }
}
